/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dto.Category;
import dto.Image;
import dto.Product;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author duonghung1369
 */
public class ProductDAO extends BaseDAO<Product> {

    private Log log;

    public ProductDAO(Class<Product> clazz) {
        super(clazz);
        log = LogFactory.getLog(ProductDAO.class);
    }

    public List getLatestProducts() throws HibernateException {
        List products = new ArrayList();
        Criteria criteria = null;
        session.beginTransaction();
        criteria = session.createCriteria(Product.class);
        criteria.add(Restrictions.ge("isShowed", Boolean.TRUE));
        products = criteria.list();
        return products;
    }

    public List getProductsByCategory(int categoryId) throws HibernateException {
        session.beginTransaction();
        Object obj = session.get(Category.class, categoryId);
        if (obj == null) {
            return null;
        }

        Category category = (Category) obj;
        return (List) category.getProducts();
    }

    public boolean updateProductQuantity(int productId, int newQuantity) {
        session.beginTransaction();
//        Object obj = session.get(type, productId);
//
//        if(obj == null) {
//            log.error("ProductDAO.updateProductQuantity(): Can't get product with id = " + productId);
//            return false;
//        }
//
//        Product p = (Product) obj;
//        p.setQuantity(newQuantity);
//        session.update(p);

        String sql = "update Product set quantity = :newQuantity where id =:id";
        Query query = session.createQuery(sql);
        query.setInteger("id", productId);
        query.setInteger("newQuantity", newQuantity);
        int result = query.executeUpdate();
        System.out.println("Update product quantity result: ==============> " + result);
        return true;
    }

    public boolean updateProduct(Product updatedProduct) {
        boolean result = false;
        session.beginTransaction();

        Object obj = get(updatedProduct.getId());

        if (obj != null) {
            Product product = (Product) obj;
            product.setName(updatedProduct.getName());
            product.setPrice(updatedProduct.getPrice());
            product.setQuantity(updatedProduct.getQuantity());
            product.setIsShowed(updatedProduct.isIsShowed());
            //product.setIsDeleted(updatedProduct.isIsDeleted());
            product.setCategory(updatedProduct.getCategory());

            List<Image> updateImage = updatedProduct.getImages();

            List<Image> images = product.getImages();
            if (images != null) {
                List<Image> updatedImages = updatedProduct.getImages();
                if (updatedImages == null) {
                    product.setImages(null);
                } else {
                    images.get(0).setUrl(updatedImages.get(0).getUrl());
                }
            }

//            //Set product for image
//            for (Image image : updateImage) {
//                image.setProduct(product);
//            }
//
//            //Update image
//            product.setImages(updateImage);

            session.update(product);
            session.getTransaction().commit();
            result = true;
        }

        return result;
    }

    public boolean deleteProductById(int id) {
        boolean result = false;
        session.beginTransaction();
        Criteria criteria = session.createCriteria(type);
        criteria.add(Restrictions.eq("id", id));
        Object obj = criteria.uniqueResult();

        if (obj != null) {
            Product product = (Product) obj;
            if (product.isIsDeleted()) {
                product.setIsDeleted(false);
            } else {
                product.setIsDeleted(true);
            }



            session.save(product);
            session.getTransaction().commit();
            result = true;
        }
        return result;
    }
}
